<script setup lang="ts">
import { useMemberStore } from '@/stores/modules/memberStore'
import { onLoad } from '@dcloudio/uni-app';
import { computed, ref } from 'vue';
import { useXtxRecommand } from '@/composables/useXtxRecommand'
import { ORDER_STATUS } from '@/constants/order';

const { safeAreaInsets } = uni.getSystemInfoSync();

// 用户信息store
const memberStore = useMemberStore();

onLoad(() => {
  if (!memberStore.profile) uni.navigateTo({ url: '/pages/login/login' });
});

// 跳转地址
const navigatorUrl = computed(() => memberStore.profile ? '/pagesMember/profile/profile' : '/pages/login/login');

// 采用推荐商品hook
const { xtxRecommandRef, xtxRecommandLoadMore, xtxRecommandRefresh } = useXtxRecommand();

// 是否处于刷新状态
const refreshing = ref(false);

// 刷新事件
const onRefresh = async () => {
  refreshing.value = true;
  await xtxRecommandRefresh();
  refreshing.value = false;
};
</script>

<template>
  <scroll-view class="viewport" scroll-y refresher-enabled :refresher-triggered="refreshing"
    @refresherrefresh="onRefresh" @scrolltolower="() => xtxRecommandLoadMore()">
    <!-- 个人资料 -->
    <view class="profile" :style="{ paddingTop: `${safeAreaInsets?.top || 0}px` }">
      <view class="overview">
        <navigator :url="navigatorUrl" hover-class="none">
          <image class="avatar" mode="aspectFill"
            :src="memberStore.profile?.avatar || 'http://yjy-xiaotuxian-dev.oss-cn-beijing.aliyuncs.com/picture/2021-04-06/db628d42-88a7-46e7-abb8-659448c33081.png'">
          </image>
        </navigator>
        <view class="meta">
          <view class="nickname">{{ memberStore.profile?.nickname || '尚未登录' }}</view>
          <navigator class="extra" :url="navigatorUrl" hover-class="none">
            <text class="tips">{{ memberStore.profile ? '修改个人资料' : '点此登录账号' }}</text>
          </navigator>
        </view>
      </view>
      <navigator class="settings" url="/pagesMember/settings/settings" hover-class="none">设置</navigator>
    </view>
    <!-- 订单选项 -->
    <view class="orders">
      <view class="title">我的订单
        <navigator class="navigator"
          :url="memberStore.profile ? '/pagesMember/profile/profile' : '/pagesOrder/list/list?type=0'"
          hover-class="none">查看全部订单
          <text class="icon-right"></text>
        </navigator>
      </view>
      <view class="section">
        <navigator class="navigator" hover-class="none" v-for="item in ORDER_STATUS.slice(0, 4)" :key="item.type"
          :class="item.icon"
          :url="memberStore.profile ? '/pagesMember/profile/profile' : `/pagesOrder/list/list?type=${item.type}`">{{
    item.text }}</navigator>
        <!-- 客服 -->
        <button class="contact icon-handset" open-type="contact">售后</button>
      </view>
    </view>
    <!-- 猜你喜欢 -->
    <view class="guess">
      <XtxRecommand ref="xtxRecommandRef" />
    </view>
  </scroll-view>
</template>

<style lang="scss">
.viewport {
  width: 100vw;
  height: 100vh;
  background-repeat: no-repeat;
  background-image: url('@/static/images/center_bg.png');
  background-size: 100% auto;
}

/* 用户信息 */
.profile {
  margin-top: 20rpx;
  position: relative;

  .overview {
    display: flex;
    height: 120rpx;
    padding: 0 36rpx;
    color: #fff;
  }

  .avatar {
    width: 120rpx;
    height: 120rpx;
    border-radius: 50%;
    background-color: #eee;
  }

  .gray {
    filter: grayscale(100%);
  }

  .meta {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: flex-start;
    line-height: 30rpx;
    padding: 16rpx 0;
    margin-left: 20rpx;
  }

  .nickname {
    max-width: 350rpx;
    margin-bottom: 16rpx;
    font-size: 30rpx;

    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .extra {
    display: flex;
    font-size: 20rpx;
  }

  .tips {
    font-size: 22rpx;
  }

  .update {
    padding: 3rpx 10rpx 1rpx;
    color: rgba(255, 255, 255, 0.8);
    border: 1rpx solid rgba(255, 255, 255, 0.8);
    margin-right: 10rpx;
    border-radius: 30rpx;
  }

  .settings {
    position: absolute;
    bottom: 0;
    right: 40rpx;
    font-size: 30rpx;
    color: #fff;
  }
}

/* 我的订单 */
.orders {
  position: relative;
  z-index: 99;
  padding: 30rpx;
  margin: 50rpx 20rpx 0;
  background-color: #fff;
  border-radius: 10rpx;
  box-shadow: 0 4rpx 6rpx rgba(240, 240, 240, 0.6);

  .title {
    height: 40rpx;
    line-height: 40rpx;
    font-size: 28rpx;
    color: #1e1e1e;

    .navigator {
      font-size: 24rpx;
      color: #939393;
      float: right;
    }
  }

  .section {
    width: 100%;
    display: flex;
    justify-content: space-between;
    padding: 40rpx 20rpx 10rpx;

    .navigator,
    .contact {
      text-align: center;
      font-size: 24rpx;
      color: #333;

      &::before {
        display: block;
        font-size: 60rpx;
        color: #ff9545;
      }

      &::after {
        border: none;
      }
    }

    .contact {
      padding: 0;
      margin: 0;
      border: 0;
      background-color: transparent;
      line-height: inherit;
    }
  }
}

/* 猜你喜欢 */
.guess {
  background-color: #f7f7f8;
  margin-top: 20rpx;
}
</style>
